import { ApiProperty } from '@nestjs/swagger';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Menu } from '../menu/menu.entity';

@Entity()
export class Role {
  @ApiProperty({ type: Number, description: 'id' })
  @PrimaryGeneratedColumn()
  public id: number;

  @ApiProperty({ type: String, description: '角色名称' })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '角色名称',
  })
  public role_name: string;

  @ApiProperty({ type: String, description: '角色权限字符串' })
  @Column({
    type: 'varchar',
    length: 20,
    nullable: false,
    unique: true,
    comment: '角色权限字符串',
  })
  public role_key: string;

  @ApiProperty({ type: Number, description: '显示顺序' })
  @Column({
    type: 'int',
    comment: '显示顺序',
  })
  public sort: number;

  @ApiProperty({
    type: String,
    description:
      '数据范围（0：全部数据 1：本部门及以下数据权限 2：本部门数据权限 3：仅本人权限）',
  })
  @Column({
    type: 'varchar',
    comment:
      '数据范围（0：全部数据 1：本部门及以下数据权限 2：本部门数据权限 3：仅本人权限）',
    default: '0',
  })
  public data_scope: string;

  @ApiProperty({ type: String, description: '角色状态(1:正常, 0: 停用)' })
  @Column({
    type: 'varchar',
    comment: '角色状态(1:正常, 0: 停用)',
  })
  public status: string;

  @ApiProperty({ type: String, description: '是否关联(0:不关联, 1: 关联)' })
  @Column({
    type: 'varchar',
    comment: '是否关联(0:不关联, 1: 关联)',
    default: '1',
  })
  public is_check_strictly: string;

  @ApiProperty({ type: String, description: '删除标志（0代表存在 1代表删除）' })
  @Column({
    type: 'varchar',
    comment: '删除标志（0代表存在 1代表删除）',
    nullable: true,
    default: '0',
  })
  public is_delete: string;

  @ApiProperty({
    type: String,
    description: '备注',
  })
  @Column({
    type: 'text',
    comment: '备注',
    nullable: true,
  })
  public remark: string;

  @ApiProperty({ type: Date, description: '创建时间' })
  @CreateDateColumn({
    type: 'timestamp',
    comment: '创建时间',
    nullable: true,
  })
  public created_date: Date;

  @ApiProperty({
    type: String,
    description: '创建人',
  })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '创建人',
    nullable: true,
  })
  public created_by: string;

  @ApiProperty({ type: Date, description: '更新时间' })
  @UpdateDateColumn({
    type: 'timestamp',
    comment: '更新时间',
    nullable: true,
  })
  public updated_date: Date;

  @ApiProperty({
    type: String,
    description: '更新人',
  })
  @Column({
    type: 'varchar',
    length: 20,
    comment: '更新人',
    nullable: true,
  })
  public updated_by: string;

  @ApiProperty({ type: Array, description: '菜单' })
  @ManyToMany(() => Menu, (menu) => menu.roles, {
    createForeignKeyConstraints: false, //避免创建外键约束
  })
  @JoinTable()
  menus: Menu[];

  constructor(partial: Partial<Role>) {
    Object.assign(this, partial);
  }
}
